package com.example.userprovider.service;

import com.example.userprovider.domain.Member;
import com.example.userprovider.dto.AuthLoginDto;
import com.example.userprovider.dto.LoginTypeEnum;
import com.example.userprovider.mapper.MemberMapper;
import org.apache.commons.lang.StringUtils;
import org.example.exception.ValidException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author
 * @date 2021-4-18 21:43
 */
@Service
public class NormalLoginProcessor extends AbstractLogin {
    @Autowired
    private MemberMapper memberMapper;
    @Override
    public Integer getLoginType() {
        return LoginTypeEnum.NORMAL.getCode();
    }

    @Override
    public void validate(AuthLoginDto dto) {
        if(StringUtils.isBlank(dto.getUsername()) || StringUtils.isBlank(dto.getPassword())) {
            throw new ValidException("账号或者密码不能为空");
        }
    }

    @Override
    public Member doProcessor(AuthLoginDto dto) {

        Member member = new Member();
        member.setUsername(dto.getUsername());
        member = memberMapper.selectUser(member);
        if(member == null ) {
            throw new ValidException("账号或密码错误");
        }
        if(!DigestUtils.md5DigestAsHex(dto.getPassword().getBytes()).equals(member.getPassword())) {
            throw new ValidException("账号或密码错误");
        }
        return member;
    }
}
